module ActiveRecord
  
  class Base

    class << self
      
      # use this when you want to validate the existence
      # of an association or the foreing key to that association
      # usually you'll only need to pass the association name, as
      # the foreign key can be inferenced as "#{association_name}_id",
      # if your foreign key does not follow the standard naming scheme
      # you can pass it as the second parameter to this method
      def validates_presence_of_association( association, foreign_key = nil )
        foreign_key ||= "#{association}_id"
        validates_presence_of( association, :if => Proc.new { |record| record.send( foreign_key ).blank? } )
        validates_presence_of( foreign_key, :if => Proc.new { |record| record.send( association ).blank? } )
      end
      
    end
    
  end

end